package com.example.android.wificar;

import android.os.Bundle;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class MainActivity extends AppCompatActivity
        implements View.OnClickListener {
    Socket socket = null;
    Button buttonF;
    Button buttonB;
    Button buttonL;
    Button buttonR;
    Button buttonC;
    Button buttonS;
    Button buttonQ;
    EditText ip;
    EditText port;
    BufferedWriter writer = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //*********界面部分调用赋值*************//
        buttonF = (Button) findViewById(R.id.buttonF);
        buttonF.setOnClickListener(this);
        buttonC = (Button) findViewById(R.id.buttonC);
        buttonC.setOnClickListener(this);
        buttonB = (Button) findViewById(R.id.buttonB);
        buttonB.setOnClickListener(this);
        buttonL = (Button) findViewById(R.id.buttonL);
        buttonL.setOnClickListener(this);
        buttonR = (Button) findViewById(R.id.buttonR);
        buttonR.setOnClickListener(this);
        buttonS = (Button) findViewById(R.id.buttonS);
        buttonS.setOnClickListener(this);
        buttonQ = (Button) findViewById(R.id.buttonQ);
        buttonQ.setOnClickListener(this);

        port = (EditText) findViewById(R.id.port);
        ip = (EditText) findViewById(R.id.ip);
    }

    //************核心功能部分*************//


    //按键筛选函数
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.buttonC:
                connect();
                break;
            case R.id.buttonF:
                send("2");
                break;
            case R.id.buttonB:
                send("8");
                break;
            case R.id.buttonL:
                send("4");
                break;
            case R.id.buttonR:
                send("6");
                break;
            case R.id.buttonS:
                send("5");
                break;
            default:
                break;
        }
    }


    //socket连接函数
    public void connect() {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    socket = new Socket(ip.getText().toString(), Integer.parseInt(port.getText().toString()));
                    writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                    Looper.prepare();
                    Toast.makeText(MainActivity.this, "建立连接成功", Toast.LENGTH_SHORT).show();
                    Looper.loop();
                } catch (IOException e) {
                    Looper.prepare();
                    Toast.makeText(MainActivity.this, "无法建立连接", Toast.LENGTH_SHORT).show();
                    Looper.loop();
                    e.printStackTrace();
                }
            }
        }
        );
        t.start();
    }

//发送函数，socket通信部分的outputstream
    public void send(String string) {
        try {
            writer.write(string + "\n");
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}





